// pages/deviceActivation/deviceActivation.js
var util = require("../../utils/util.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: false,//按钮的loading图标
    qrcode_no: '',//设备码
    device_info: '',//设备详情
    showModel: false,//激活成功弹窗
    addressMenuIsShow: false,
    value_sel: [0, 0, 0], //对应数组下标
    province_s: [],
    city_s: [],
    area_s: [],
    areaInfo_name: [], //选中地区名称
    area_id_Info: [], //选中的地区id信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //二维码进入获取参数
    if (util.isExitsVariable(options.scene)) {
      var scene = util.sceneToArr(decodeURIComponent(options.scene));
      console.log(scene, "scne")
      if (util.isExitsVariable(scene.device_no)) {
        that.setData({
          qrcode_no: scene.device_no
        })
        that.getDeviceInfo(scene.device_no);
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    that.headid = this.selectComponent("#addre_id"); //引入地址    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  //扫码
  scan(){
    var that = this;
    wx.scanCode({
      success: (res) => {
        app.showToast('扫码成功');
        console.log(res)
        // 截取设备编号
        let str = res.result;
        let result = '';
        if (str.indexOf('device_no=') > -1) {
          let strs = str.split('?device_no=');
          result = strs[1];
        }
        that.setData({
          qrcode_no: result
        })
        that.getDeviceInfo(result);
      },
      fail: (res) => {
        app.showToast('扫码失败，请重试！');
      }
    })
  },
  //获取设备详情
  getDeviceInfo(code){
    var that = this;
    app.ajax({
      url: 'User/Equipmentlists/getEqInfo',
      method: "POST",
      data: {
        qrcode_no: code
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            device_info: res.data.data,
            areaInfo_name: [res.data.data.province, res.data.data.city, res.data.data.area],
            area_id_Info: [res.data.data.province_code, res.data.data.city_code, res.data.data.area_code],
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  //点击键盘提交
  searchInfo(e){
    var value = e.detail.value;
    this.getDeviceInfo(value);
  },
  //提交数据
  submit(e){
    var that = this;
    var val = e.detail.value;
    var areaInfo_name = that.data.areaInfo_name;
    var area_id_Info = that.data.area_id_Info;
    if (!val.device_no){
      app.showToast('请先输入设备编号或扫描二维码');
      return false
    }
    that.setData({
      isLoading: true
    })
    app.ajax({
      url: 'User/Equipmentlists/activeEquipment',
      method: "POST",
      data: {
        qrcode_no: val.device_no,
        contact: val.contact,
        contact_number: val.contact_number,
        address: val.address,
        province: areaInfo_name[0],
        city: areaInfo_name[1],
        area: areaInfo_name[2],
        province_code: area_id_Info[0],
        city_code: area_id_Info[1],
        area_code: area_id_Info[2],
      },
      success: function (res) {
        that.setData({
          isLoading: false
        })
        if (res.data.code == 1000) {
          that.setData({
            showModel: true
          })
        }else{
          app.showToast(res.data.message);
        }
      },
    })
    
  },

  //关闭弹窗
  closeModel(){
    this.setData({
      showModel: false,
      qrcode_no: '',//设备码
      device_info: '',//设备详情
    })
  },
  //查看详情
  navInfo(){
    this.setData({
      showModel: false,
      qrcode_no: '',//设备码
      device_info: '',//设备详情
    })
    wx.navigateTo({
      url: '/pages/facilityDetail/facilityDetail?equipment_id=' + this.data.device_info.equipment_id,
    })
  },
  //去商城
  navShop(){
    wx.switchTab({
      url: '/pages/shopping/shoppingHome/shoppingHome',
    })
  },
  //打开地图选择
  openAddress() {
    var that = this;
    this.headid.startAddressAnimation(true);
  },
  // 一下为地址(地图)选择.........................................................................................
  openaddshoose() {
    var that = this;
    wx.chooseLocation({
      success: (res) => {
        console.log('地址选择结果', res);
        ch_longitude = res.longitude;
        ch_latitude = res.latitude;
        that.setData({
          add_ress: res.name
        })
      }
    });
  },
  getSure(data) {
    console.log('确定事件获取地址', data);
    var that = this;
    that.setData({
      areaInfo_name: data.detail.areaName,
      area_id_Info: data.detail.areaId,
      valueName: data.detail.areaName
    })
  },
})